package com.mf.all.controller.basic;


import com.mf.all.entity.req.basic.ReqPermissionByRole;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.service.ServiceRolePermission;
import com.mf.all.utils.UtilController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoPermission;
import com.mf.all.entity.req.user.ReqPermission;
import com.mf.all.service.ServicePermission;
import com.mf.all.entity.req.search.user.SearchReqPermission;

import java.util.List;

/**
 * 权限表
 *
 * @date 2025-04-02 22:36:49
 */
@RestController
@RequestMapping("permission")
@Tag(name = "权限表")
public class ControllerPermission {
    @Resource
    private ServicePermission servicePermission;

    @Resource
    private ServiceRolePermission serviceRolePermission;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("permission:search")
    public Result<Page<DtoPermission>> findPage(@RequestBody SearchReqPermission searchReqPermission) {
        return Result.success(servicePermission.findPageByParams(searchReqPermission));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("permission:search")
    public Result<List<DtoPermission>> list(@RequestBody SearchReqPermission searchReqPermission) {
        return Result.success(servicePermission.findListByParams(searchReqPermission));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("permission:save")
    @Transactional
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqPermission reqPermission) {
        servicePermission.validateId(reqPermission.getPid());
        servicePermission.save(UtilController.setBo(reqPermission, DtoPermission.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("permission:update")
    @Transactional
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqPermission reqPermission) {
        servicePermission.validateId(reqPermission.getPid());
        servicePermission.updateById(UtilController.setBo(reqPermission, DtoPermission.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    @SaCheckPermission("permission:delete")
    public Result<Void> delete(@RequestParam("id") Integer id) {
        servicePermission.deleteById(id);
        return Result.success();
    }

    @Operation(summary = "角色绑定权限")
    @SaCheckPermission("permission:permissionByRole")
    @PostMapping("/permissionByRole")
    public Result<Void> permissionByRole(@Validated @RequestBody ReqPermissionByRole reqPermissionByRole) {
        serviceRolePermission.permissionByRole(reqPermissionByRole);
        return Result.success();
    }

    @Operation(summary = "查询所有权限(树结构)")
    @SaCheckPermission("permission:search")
    @GetMapping("/permissionByTree")
    public Result<List<DtoPermission>> permissionByTree() {
        return Result.success(servicePermission.permissionByTree());
    }


}
